import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.*;
import java.io.IOException;

import javax.swing.*;

public class MainFrame extends JFrame implements KeyListener {

	private int screenHeight = 0;
	private int screenWidth = 0;
	
	public MainFrame(String windowName)
	{
		super(windowName);
	
		JMenuBar menuBar = new JMenuBar();
		
		menuBar.add(setFileBar());
		menuBar.add(threeDBar());
		menuBar.add(setHelpBar());
		setJMenuBar(menuBar);
		
		getScreenSize();
	
		addKeyListener(this);
		this.setVisible(true);
		this.setSize(800,600);
		this.setLocation(screenWidth,0);
		this.setDefaultCloseOperation(EXIT_ON_CLOSE);
	}
	
	public JMenu setFileBar()
	{
		JMenu fileMenu = new JMenu("File");
		JMenuItem newFile = new JMenuItem("New");
		newFile.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_N,ActionEvent.CTRL_MASK));
		fileMenu.add(newFile);
		
		JMenuItem saveAs = new JMenuItem("Save As");
		saveAs.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S,ActionEvent.CTRL_MASK));
		fileMenu.add(saveAs);
		
		JMenuItem print = new JMenuItem("Print");
		print.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_P,ActionEvent.CTRL_MASK));
		fileMenu.add(print);
		
		JMenuItem exit = new JMenuItem("Exit TinCD");
		exit.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_Q,ActionEvent.CTRL_MASK));
		fileMenu.add(exit);
		
		return fileMenu;
	}
	
	public JMenu setHelpBar()
	{
		JMenu helpMenu = new JMenu("Help");
		
		JMenuItem genHelp = new JMenuItem("General Help");
		genHelp.setAccelerator(KeyStroke.getKeyStroke("F1"));
		helpMenu.add(genHelp);
		
		JMenuItem aboutHelp = new JMenuItem("About TinCD");
		aboutHelp.setAccelerator(KeyStroke.getKeyStroke("F2"));
		helpMenu.add(aboutHelp);
		
		return helpMenu;
	}
	
	public JMenu threeDBar()
	{
		JMenu threeD = new JMenu("3D Drawing");
		
		JMenuItem windowTest = new JMenuItem("Window Test");
		windowTest.setAccelerator(KeyStroke.getKeyStroke("F3"));
		threeD.add(windowTest);
		
		JMenuItem sphereCam = new JMenuItem("Spherical Camera Test");
		sphereCam.setAccelerator(KeyStroke.getKeyStroke("F4"));
		threeD.add(sphereCam);
		
		JMenuItem povRay = new JMenuItem("Export and Run in POVRay");
		povRay.setAccelerator(KeyStroke.getKeyStroke("F5"));
		threeD.add(povRay);
		
		return threeD;
		
	}
	
	public void getScreenSize()
	{
		Toolkit javaToolKit = Toolkit.getDefaultToolkit();
		Dimension screenSize = javaToolKit.getScreenSize();
		screenHeight = screenSize.height;
		screenWidth = screenSize.width/4;
		
	}

	public void fileMenuHandler(int pressed, int modifier)
	{
		
		if ( pressed == KeyEvent.VK_N && (modifier == KeyEvent.CTRL_MASK))
			{	
	    		System.out.println("New Canvas");
			}
		
		if ( pressed == KeyEvent.VK_S && (modifier == KeyEvent.CTRL_MASK))
			{	
	    		System.out.println("Save as");
			}
		
		if ( pressed == KeyEvent.VK_P && (modifier == KeyEvent.CTRL_MASK))
			{	
	    		System.out.println("Print");
			}
		
		if ( pressed == KeyEvent.VK_Q && (modifier == KeyEvent.CTRL_MASK))
			{	
	    		System.out.println("Quit");
			}
					
	}
	
	public void helpMenuHandler(int pressed)
	{
		if (pressed == KeyEvent.VK_F1)
		{
	    	System.out.println("General Help");
	    	
		}
		
		if (pressed == KeyEvent.VK_F2)
		{
	    	System.out.println("About TinCD");
	    	
		}
	}
	
	public void initJOGL(int pressed)
	{
	    if (pressed == KeyEvent.VK_F3)
		{
	    	JOGLFrame JOGLWin = new JOGLFrame(1);
	    	
		}
	    
	    if (pressed == KeyEvent.VK_F4)
	    {
	    	JOGLFrame JOGLWin = new JOGLFrame(0);
	    }
		
	}
	
	public void initPOVRay (int pressed) throws IOException
	{
		if (pressed == KeyEvent.VK_F5)
		{
			POVRay initPOV = new POVRay();
		}
	}

	public void keyPressed(KeyEvent event) {
		
		int keyCode = event.getKeyCode();
		int modifier = event.getModifiers();
		
		fileMenuHandler(keyCode,modifier);
		helpMenuHandler(keyCode);
		
		initJOGL(keyCode);
		try {
			initPOVRay(keyCode);
		} catch (IOException e) {
			//Dialog pane here to display error print out for now
			System.out.println("POVRay failed to initialize. Please check your PATH variable and" +
					"filename!");
			e.printStackTrace();
		}
	}

	public void keyReleased(KeyEvent arg0) {
				
	}

	public void keyTyped(KeyEvent event) {
		
						
	}
}